﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyFractals
{
    public partial class Form2: Form 
    {

        Pen pen;
        System.Drawing.Graphics formGraphics;
        public float a1 = 0, a2 = 0, da1 = 0, da2 = 0, rp1 = 0, rp2 = 0, K = 0, r1 = 0, r2 = 0, lw = 0;
        public float p1x =0 ,p1y=0, p2x = 0, p2y = 0;
        public Color bgcolor, linecolor;
        public int zoom=0, speed=0;
        private int checkflag = 1;


        public Form2(float form1_da1, float form1_da2, float form1_rp1, float form1_rp2, float form1_lw,
            Color form1_bgcolor,Color form1_linecolor,int zoom,int speed)
        {
            InitializeComponent();

            this.da1 = form1_da1;
            this.da2 = form1_da2;
            this.rp1 = form1_rp1;
            this.rp2 = form1_rp2;
            this.lw = form1_lw;
            this.linecolor = form1_linecolor;
            this.bgcolor = form1_bgcolor;
            float.TryParse((Screen.PrimaryScreen.Bounds.Size.Height * (1 + (zoom - 3.0) / 5)).ToString(),out K);
            this.r1 = this.rp1 * K / 100;
            this.r2 = this.rp2 * K / 100;
            this.BackColor = this.bgcolor;
            this.speed = speed;
            this.zoom = zoom;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            this.DoubleBuffered = true;
            this.Size = Screen.PrimaryScreen.Bounds.Size;
            this.Location = new Point(0,0);
            formGraphics = this.CreateGraphics();
            formGraphics.TranslateTransform(this.Size.Width / 2, this.Size.Height / 2);
            formGraphics.PageUnit = GraphicsUnit.Pixel;
            pen = new Pen(linecolor,lw);
            switch(speed)
            {
                case 1:
                    timer1.Interval = 1000;
                    break;
                case 2:
                    timer1.Interval = 300;
                    break;
                case 3:
                    timer1.Interval = 100;
                    break;
                case 4:
                    timer1.Interval = 50;
                    break;
                case 5:
                    timer1.Interval = 10;
                    break;
            }

            timer1.Enabled = true;
        }


        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                timer1.Enabled = false;
                pen.Dispose();
                formGraphics.Dispose();
                this.Close();
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                timer1.Enabled = !timer1.Enabled;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Middle)
            {
                Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));
                bmp.Save("Image-" + DateTime.Now.ToString("yyyyMMdd_hh-mm-ss") + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                bmp.Dispose();
                g.Dispose();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            a1 += da1; a1 %= 360;
            a2 += da2; a2 %= 360;

            float.TryParse((r1 * Math.Cos(2 * Math.PI * a1 / 360)).ToString(),out p1x);
            float.TryParse((r1 * Math.Sin(2 * Math.PI * a1 / 360)).ToString(), out p1y);
            float.TryParse((r2 * Math.Cos(2 * Math.PI * a2 / 360)).ToString(), out p2x);
            float.TryParse((r2 * Math.Sin(2 * Math.PI * a2 / 360)).ToString(), out p2y);

            formGraphics.DrawLine(pen, p1x, p1y, p2x, p2y);

        }
    }
}
